<template>
  <div class="addGoods">
    <el-card>
      <el-alert
        center
        show-icon
        title="添加商品信息"
        type="info">
      </el-alert>
      <el-steps :active="active - 0" :space="200" align-center finish-status="success">
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <el-form ref="form" :model="form" :rules="rules">
        <el-tabs v-model="active" :before-leave="beforeleave" :tab-position="tabPosition"
                 style="height: 100%;" @tab-click="handleClick">
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="form.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="form.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="form.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="form.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader
                v-model="form.goods_cat"
                :options="attrs"
                :props=catePropsl
                expand-trigger="hover"
                @change="handleChange"></el-cascader>
            </el-form-item>

          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <el-form-item v-for="(item) in goodsType" :key="item.id" :label="item.attr_name">
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox v-for="(a,i) in item.attr_vals" :key="i" :label="a" border></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
            <el-form-item v-for="item in newTable" :key="item.attr_id" :label="item.attr_name">
              <el-input :key="item.attr_id" v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <el-upload
              :headers="headersObj"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              action="https://lianghj.top:8888/api/private/v1/update"
              class="upload-demo"
              list-type="picture">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <el-input v-model="form.goods_introduce" style="height: 200px" type="textarea"></el-input>
            <el-button @click="addGoods">添加商品</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>

  </div>
</template>

<script>
import { addGoodslist, getCategories, getGoodsType } from '@/api/goods'
import store from '@/store'

export default {
  name: 'hm-addGoods',
  data () {
    return {
      textarea: '',
      active: '0',
      newactive: 0,
      tabPosition: 'left',
      form: {
        goods_name: '',
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0,
        goods_cat: [],
        goods_introduce: '',
        pics: [],
        attrs: []
      },
      attrs: [],
      rules: {
        goods_name: [{
          required: true,
          message: '请输入内容',
          trigger: ['blur', 'change']
        }],
        goods_price: [{
          required: true,
          message: '请输入内容',
          trigger: ['blur', 'change']
        }],
        goods_number: [{
          required: true,
          message: '请输入内容',
          trigger: ['blur', 'change']
        }],
        goods_weight: [{
          required: true,
          message: '请输入内容',
          trigger: ['blur', 'change']
        }],
        goods_cat: [{
          required: true,
          message: '请输入内容',
          trigger: ['blur', 'change']
        }]
      },
      catePropsl: {
        label: 'cat_name',
        children: 'children',
        value: 'cat_id'
      },
      goodsType: [],
      newTable: [],
      headersObj: {
        Authorization: store.state.user.token
      }
    }
  },
  created () {
    this.getCategories()
  },
  computed: {
    cateID () {
      if (this.form.goods_cat.length === 3) {
        return this.form.goods_cat[2]
      }
      return null
    }
  },
  methods: {
    // 提交
    async addGoods () {
      this.form.attrs = this.goodsType
      this.form.goods_cat = this.form.goods_cat.join(',')
      const res = await addGoodslist(this.form)
      if (res.meta.status === 201) {
        await this.getCategories()
        await this.$router.push('goodslist')
        this.$message.success(res.meta.msg)
      } else {
        return this.$message.error(res.meta.msg)
      }
    },
    // 图片上传区域
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file)
    },
    handleChange (value) {
      console.log(this.form.goods_cat)
      if (this.form.goods_cat.length !== 3) {
        this.form.goods_cat = []
      }
    },
    async getCategories () {
      const res = await getCategories()
      this.attrs = res.data
    },
    async handleClick () {
      console.log(this.active)
      if (this.active === '1') {
        const res = await getGoodsType(this.cateID, {
          sel: 'many'
        })
        res.data.forEach(item => {
          item.attr_vals = item.attr_vals.length === 0 ? [] : item.attr_vals.split(' ')
        })
        this.goodsType = res.data
      } else if (this.active === '2') {
        const res = await getGoodsType(this.cateID, {
          sel: 'only'
        })
        this.newTable = res.data
        console.log(this.newTable)
      }
    },
    beforeleave (active, oldName) {
      if (oldName === '0' && !this.form.goods_cat.length) {
        this.$message('请先选择商品分类')
        return false
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.height {
  height: 100px;
}

.addGoods {
  .el-steps {
    display: flex;
    justify-content: start;
  }
}
</style>
